﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Scoreboard.Controls
{
	public class NavigationPane : ContentControl
	{		
		FrameworkElement _canvas;
		FrameworkElement _content;

		private bool _dragging;
		private Point _previousPosition;
		
		public NavigationPane()
		{
			this.DefaultStyleKey = typeof(NavigationPane);

			Loaded += new RoutedEventHandler(NavigationPane_Loaded);
		}

		private void NavigationPane_Loaded(object sender, RoutedEventArgs e)
		{
			AdjustCursor();

			_canvas.SizeChanged += new SizeChangedEventHandler(ElementSizeChanged);
			_content.SizeChanged += new SizeChangedEventHandler(ElementSizeChanged);
		}		

		private void ElementSizeChanged(object sender, SizeChangedEventArgs e)
		{
			AdjustCursor();
		}

		public void Reset()
		{
			_dragging = false;
			_previousPosition = new Point(0, 0);
			Canvas.SetTop(_content, 0);
			Canvas.SetLeft(_content, 0);
		}

		public override void OnApplyTemplate()
		{
			//_contentContainer = GetTemplateChild("contentContainer") as FrameworkElement;
			_canvas = GetTemplateChild("canvas") as FrameworkElement;
			_content = GetTemplateChild("content") as FrameworkElement;

			_content.MouseLeftButtonDown += _content_MouseLeftButtonDown;
			_content.MouseLeftButtonUp += _content_MouseLeftButtonUp;
			_content.MouseMove += _content_MouseMove;			

			base.OnApplyTemplate();
		}			

		void _content_MouseMove(object sender, MouseEventArgs e)
		{
			if (_dragging)
			{
				var position = e.GetPosition(this);

				if (_content.ActualWidth > _canvas.ActualWidth)
				{
					var leftNewValue = Canvas.GetLeft(_content) + (position.X - _previousPosition.X) * 2;
					var minLeftValue = _canvas.ActualWidth - _content.ActualWidth;
					if (leftNewValue > 0)
					{
						leftNewValue = 0;
					}
					else if (leftNewValue < minLeftValue)
					{
						leftNewValue = minLeftValue;
					}
					Canvas.SetLeft(_content, leftNewValue);
				}

				if (_content.ActualHeight > _canvas.ActualHeight)
				{
					var topNewValue = Canvas.GetTop(_content) + (position.Y - _previousPosition.Y) * 2;
					var minTopValue = _canvas.ActualHeight - _content.ActualHeight;
					if (topNewValue > 0)
					{
						topNewValue = 0;
					}
					else if (topNewValue < minTopValue)
					{
						topNewValue = minTopValue;
					}
					Canvas.SetTop(_content, topNewValue);
				}

				_previousPosition = position;
			}
		}

		void _content_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_dragging = false;
		}

		void _content_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			_dragging = true;
			_previousPosition = e.GetPosition(this);
		}

		private void AdjustCursor()
		{
			if (_content.ActualWidth > _canvas.ActualWidth
				|| _content.ActualHeight > _canvas.ActualHeight)
			{
				Cursor = Cursors.SizeNWSE;
			}
			else
			{
				Cursor = Cursors.Arrow;
			}
		}
	}
}
